package Week03.Day01;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * 学生管理程序的界面
 */
public class StudentUI {
    private SuperArray ary = new SuperArray();
    private Scanner scan = new Scanner(System.in);
    public StudentUI(){
        ary.add(new Student("张飞","男",20));
        ary.add(new Student("关羽","男",20));
        ary.add(new Student("刘备","女",30));
        menu();
    }
    /**
     * 菜单界面
     */
    public void menu(){
        System.out.println("欢迎使用学生管理程序");
        while(true){
            System.out.println("请选择：1-查看所有学生 2-增加 3-修改 4-删除 5-搜索 6-退出");
            int input = 0;
            try {
                input = scan.nextInt();
            }catch (InputMismatchException e){
                System.out.println("输入错误");
                scan.next();
                continue;
            }
            if(input == 1){
                show();
            }else if(input == 2){
                add();
            }else if(input == 3){
                update();
            }else if(input == 4){
                remove();
            }else if(input == 5) {
                search();
            }else if(input == 6){
                System.out.println("退出程序");
                System.exit(0);
            }
        }
    }
    /**
     * 显示所有学生
     */
    public void show(){
        for(int i = 0 ;i < ary.size();i++){
            Student student = (Student)ary.get(i);
            System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
        }
    }
    /**
     * 增加学生
     */
    public void add() {
        System.out.println("请输入学生的姓名");
        String name = scan.next();
        name.matches("^[\u4e00-\u9fa5]{2,3}$");
       boolean result = name.matches("^[\u4e00-\u9fa5]{2,3}$");
       if (result == true){
           System.out.println("姓名录入成功");
       }else {
           System.out.println("名字输入有误，请按规定输入");
           return;
       }
        System.out.println("请输入学生的性别");
        String gender = scan.next();
        if(gender.equals("男") || gender.equals("女")){
            System.out.println("性别录入成功");
        }else {
            System.out.println("性别输入有误，请按规定输入");
            return;
        }
        int age = 0;
        while (true) {
            try {
                System.out.println("请输入学生的年龄");
                age = scan.nextInt();
                break;
            } catch (InputMismatchException e) {
                System.out.println("输入错误,请重新输入");
                scan.next();
            }
        }
        if((int)age <=30 &&(int)age >= 18){
            ary.add(new Student(name, gender, age));
            System.out.println("添加成功");
            System.out.println("年龄修改成功");
        }else {
            System.out.println("年龄超出界限");
        }
    }
    /**
     * 修改学生
     */
    public void update(){
        System.out.println("输入学生的名字");
        String name =scan.next();
        for (int i = 0; i < ary.size(); i++) {
            Student student = (Student) ary.get(i);
            if (student.getName().equals(name)) {
                System.out.println("请选择修改的内容 1-名字,2-性别,3-年龄");
                int input = 0;
                while (true) {
                    try {
                        input = scan.nextInt();
                        break;
                    } catch (InputMismatchException e) {
                        System.out.println("输入错误,请重新输入");
                        scan.next();
                    }
                }
                if (input == 1) {
                    System.out.println("请输入修改后的姓名");
                    String name1 = scan.next();
                    name1.matches("^[\u4e00-\u9fa5]{2,3}$");
                    boolean result = name1.matches("^[\u4e00-\u9fa5]{2,3}$");
                    if (result == true){
                        student.setName(name1);
                        System.out.println("姓名修改成功");
                    }else {
                        System.out.println("名字输入有误，请按规定输入");
                        return;
                    }
                } else if (input == 2) {
                    System.out.println("请输入修改后的性别");
                    String gender1 = scan.next();
                    if(gender1.equals("男") || gender1.equals("女")){
                        student.setGender(gender1);
                        System.out.println("性别修改成功");
                    }else {
                        System.out.println("性别输入有误，请按规定输入");
                    }
                }else if(input == 3) {
                    int age1 = 0;
                    while (true) {
                        try {
                            System.out.println("请输入修改后的年龄");
                            age1 = scan.nextInt();
                            break;
                        } catch (InputMismatchException e) {
                            System.out.println("输入错误,请重新输入");
                            scan.next();
                        }
                    }
                    if((int)age1 <=30 &&(int)age1 >= 18){
                        student.setAge(age1);
                        System.out.println("年龄修改成功");
                    }else {
                        System.out.println("年龄超出界限");
                    }
                }
                return;
            }
        }
        System.out.println("没有找到" + name);
    }
    /**
     * 删除学生
     */
    public void remove(){
        System.out.println("请输入删除学生的名字：");
        String name = scan.next();
        for (int i = 0; i < ary.size(); i++) {
            Student student = (Student) ary.get(i);
            if (name.equals(student.getName())){
                ary.remove(i);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("没有找到" + name);
    }
    /**
     * 搜索学生信息
     */
    public void search() {
        System.out.println("请选择搜索方式: 1-姓名 2-性别 3-年龄");
        int input = 0;
        while (true) {
            try {
                input = scan.nextInt();
                break;
            } catch (InputMismatchException e) {
                System.out.println("输入错误,请重新输入");
                scan.next();
            }
        }
        if (input == 1) {
            System.out.println("请输入被查询学生的姓名");
            String name = scan.next();
            for (int i = 0; i < ary.size(); i++) {
                Student student = (Student) ary.get(i);
                if (name.equals(student.getName())) {
                    System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                }
            }
                System.out.println("查无此人");
        } else if (input == 2) {
            System.out.println("请输入被查询学生的性别");
            String gender = scan.next();
            int index= 0;
            for (int i = 0; i < ary.size(); i++) {
                Student student = (Student) ary.get(i);
                if (gender.equals(student.getGender())) {
                    System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                    index++;
                }
            }
            if (index == 0 ) {
                System.out.println("输入性别有误");
                return;
            }
        }
        if (input == 3) {
            System.out.println("请输入被查询学生的年龄");
            int age = 0;
            int index = 0;
            try {
                age = scan.nextInt();
            }catch (InputMismatchException e){
                System.out.println("输入错误,请重新输入");
                scan.next();
                return;
            }
            for (int i = 0; i < ary.size(); i++) {
                Student student = (Student) ary.get(i);
                if (age == student.getAge()) {
                    System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                    index++;
                }
            }
            if (index == 0) {
                System.out.println("没有该年龄的学生");
            }
        }
    }
    public static void main(String[] args) {
        new StudentUI();
    }
}
